% Normalize stock data by mutual dates
% Input: date_A, stock_A, date_B, stock_B
% Output: intersect_date, intersect_price
function [intersect_date intersect_price intersect_volume] = getIntersectOfStockTable(numOfStocks, date_A, price_stock_A, volume_A, date_B, price_stock_B, volume_B)

    % get min value of date_A, date_B and get the relative value
    min_value = min(min(date_A), min(date_B));
    date_A = date_A - min_value;
    date_B = date_B - min_value;
    
    % check each element of a list of dates whatever belongs to the other
    check_member_A_in_B = ismember(date_A, date_B);
    check_member_B_in_A = ismember(date_B, date_A);
    
    stock_A_table = [check_member_A_in_B date_A price_stock_A volume_A];
    stock_B_table = [check_member_B_in_A date_B price_stock_B volume_B];    
    
    % get all the mutual date of two data of stocks
    intersect_table_A = stock_A_table(stock_A_table(:, 1) == 1, :);
    intersect_table_B = stock_B_table(stock_B_table(:, 1) == 1, :);
    
    intersect_date = intersect_table_A(:, 2) + min_value;
    
    intersect_price = [intersect_table_A(:, 3:(numOfStocks + 3 - 1)) intersect_table_B(:, 3)];
    intersect_volume = [intersect_table_A(:, (numOfStocks + 3):end) intersect_table_B(:, 4)];
end